package com.example.baselibrary.util.router;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import cc.vv.lklibrary.log.LogOperate;
import cc.vv.lkrouter.router.RouterActivity;
import cc.vv.lkrouter.router.RouterV4Fragment;

/**
 * 路由中转中心工具类
 * Created by PC on 2018/4/3.
 *
 * @author sun.li
 */

public class RouterTransferCenterUtil {

    private static RouterTransferCenterUtil mInstance;

    public static RouterTransferCenterUtil getInstance() {
        if (null == mInstance) {
            synchronized (RouterTransferCenterUtil.class) {
                if (null == mInstance) {
                    mInstance = new RouterTransferCenterUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取路由中的Intent跳转对象
     *
     * @param context 上下文
     * @param key     路由Activity对应的key，“RouteActivityIntentResourceKey常量类中寻找”
     */
    public Intent getRouterIntent(Context context, String key) {
        Intent intent = null;
        if (null != context) {
            if (!TextUtils.isEmpty(key)) {
                try {
                    intent = RouterActivity.getinstance().invok(context, key);
                } catch (Exception e) {
                    LogOperate.e(e.getMessage(), e);
                    intent = null;
                }
            }
            if (null == intent) {
                try {
                    /*此处主要为了防止单模块运行时，未注册路由的Intent跳转崩溃*/
                    intent = ((FragmentActivity) context).getIntent();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return intent;
    }

    /**
     * 获取常规的Intent跳转对象
     */
    public Intent getConventionalIntent(Context context, Class classz) {
        Intent intent = null;
        if (null != context) {
            if (null != classz) {
                try {
                    intent = new Intent(context, classz);
                } catch (Exception e) {
                    LogOperate.e(e.getMessage(), e);
                    intent = null;
                }
            }
            if (null == intent) {
                try {
                    /*此处主要为了防止单模块运行时，未注册路由的Intent跳转崩溃*/
                    intent = ((FragmentActivity) context).getIntent();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return intent;
    }

    /**
     * 获取路由中的Fragment-Class对象
     *
     * @param key 路由Activity对应的key，“RouteFragmentIntentResourceKey常量类中寻找”
     */
    public Class<? extends Fragment> getRouterFragmentClass(String key) {
        Class<? extends Fragment> fragmentClass = null;
        if (!TextUtils.isEmpty(key)) {
            try {
                fragmentClass = RouterV4Fragment.getInstance().invokV(key);
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
                fragmentClass = null;
            }
        }
        if (null == fragmentClass) {
            try {
                fragmentClass = new Fragment().getClass();
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
            }
        }
        return fragmentClass;
    }

    /**
     * 获取路由中的Fragment对象
     *
     * @param key 路由Activity对应的key，“RouteFragmentIntentResourceKey常量类中寻找”
     */
    public Fragment getRouterFragment(String key) {
        Fragment fragment = null;
        if (!TextUtils.isEmpty(key)) {
            try {
                fragment = RouterV4Fragment.getInstance().invokT(key);
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
                fragment = null;
            }
        }
        if (null == fragment) {
            fragment = new Fragment();
        }
        return fragment;
    }
}
